package cn.virens.web.components.open.wxma.session;

import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.virens.web.components.open.session.WxSession;
import cn.virens.web.components.open.session.WxSessionManager;

/**
 * 微信Session包装的消息体
 * 
 * @author virens
 */
public class WxMaSessionMessage extends WxMaMessage {
	private static final long serialVersionUID = -922914355378995537L;

	private final Map<String, Object> attribute;
	private final WxMaMessage instance;
	private final String appid;

	private WxSession session;

	public WxMaSessionMessage(WxMaMessage instance, Map<String, Object> attribute, String appid) {
		this.attribute = attribute;
		this.instance = instance;
		this.appid = appid;
	}

	public String getAppid() {
		return appid;
	}

	@SuppressWarnings("unchecked")
	public <T> T getAttr(T key) {
		return (T) attribute.get(key);
	}

	@SuppressWarnings("unchecked")
	public <T> T putAttr(String key, T value) {
		return (T) attribute.put(key, value);
	}

	/**
	 * 保存session
	 * 
	 * @param sessionManager Session管理器
	 */
	public void putSession(WxSessionManager sessionManager) {
		if (session != null && sessionManager != null) {
			sessionManager.putSession(getFromUser(), session);
		}
	}

	/**
	 * 获取Session(如果不存在会创建)
	 * 
	 * @param  sessionManager Session管理器
	 * @return
	 */
	public WxSession getSession(WxSessionManager sessionManager) {
		return getSession(sessionManager, true);
	}

	/**
	 * 获取Session
	 * 
	 * @param  sessionManager Session管理器
	 * @return
	 */
	public synchronized WxSession getSession(WxSessionManager sessionManager, boolean create) {
		if (sessionManager != null && (session == null || session.isExpired())) {
			return session = sessionManager.getSession(getFromUser(), create);
		} else {
			return session;
		}
	}

	@Override
	public String getContent() {
		return instance.getContent();
	}

	@Override
	public Integer getCreateTime() {
		return instance.getCreateTime();
	}

	@Override
	public String getEncrypt() {
		return instance.getEncrypt();
	}

	@Override
	public String getEvent() {
		return instance.getEvent();
	}

	@Override
	public String getFromUser() {
		return instance.getFromUser();
	}

	@Override
	public String getMediaId() {
		return instance.getMediaId();
	}

	@Override
	public String getMsgDataFormat() {
		return instance.getMsgDataFormat();
	}

	@Override
	public Long getMsgId() {
		return instance.getMsgId();
	}

	@Override
	public String getMsgType() {
		return instance.getMsgType();
	}

	@Override
	public String getPagePath() {
		return instance.getPagePath();
	}

	@Override
	public String getPicUrl() {
		return instance.getPicUrl();
	}

	@Override
	public String getSessionFrom() {
		return instance.getSessionFrom();
	}

	@Override
	public String getThumbMediaId() {
		return instance.getThumbMediaId();
	}

	@Override
	public String getThumbUrl() {
		return instance.getThumbUrl();
	}

	@Override
	public String getTitle() {
		return instance.getTitle();
	}

	@Override
	public String getToUser() {
		return instance.getToUser();
	}

	@Override
	public String toString() {
		return JSON.toJSONString(instance);
	}

	@Override
	public int hashCode() {
		return instance.hashCode();
	}

	@Override
	public boolean equals(Object o) {
		return instance.equals(o);
	}

}
